const Benchmark = require('benchmark');
const suite = new Benchmark.Suite;

// 添加测试
suite
  .add('forEach', function() {
    const array = [1, 2, 3, 4, 5];
    array.forEach((num, index) => {
      array[index] = num * 2;
    });
  })
  .add('for loop', function() {
    const array = [1, 2, 3, 4, 5];
    for (let i = 0; i < array.length; i++) {
      array[i] = array[i] * 2;
    }
  })
  // 添加监听器
  .on('cycle', function(event) {
    console.log(String(event.target));
  })
  .on('complete', function() {
    console.log('Fastest is ' + this.filter('fastest').map('name'));
  })
  // 运行测试
  .run({ 'async': true });
